﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies.Takenlijst_windows
{
    public partial class Addlid : UserControl
    {
        //projectID uit vorige scherm doorgestuurd
        private int projectID;
        //lijst met alle projectleden (IDs)
        private string[] idList = new string[10];
        //counter voor de IDs
        private int counter = 0;

        public Addlid()
        {

        }

        public Addlid(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
            getIds();
        }

        private void getIds()
        {
            try
            {
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand retrieveIds = new MySqlCommand("SELECT UserID FROM project_members WHERE ProjectID='"+projectID+"'", grabConnection);
                grabConnection.Open();
                MySqlDataReader getIds = retrieveIds.ExecuteReader();

                while (getIds.Read())
                {
                    if (getIds["UserID"].ToString() == null)
                    {
                        break;
                    }

                    idList[counter] = getIds["UserID"].ToString();
                    counter++;
                }
                //objecten sluiten en counter resetten
                getIds.Close();
                counter = 0;
                grabConnection.Close();

                //IDs omzetten naar namen
                if (idList[0] != null)
                {
                    getNames();
                }
                else
                {
                    MessageBox.Show("Foutmelding: er kunnen geen Ids opgehaald worden");
                }
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void getNames()
        {
            MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
            grabConnection.Open();

            for (int i = 0; i <= idList.Length; i++)
            {
                if (idList[i] == null)
                {
                    break;
                }
                
                MySqlCommand retrieveUsers = new MySqlCommand("SELECT Naam FROM confirmed_users WHERE UserID='"+idList[i]+"'", grabConnection);
                MySqlDataReader getNames = retrieveUsers.ExecuteReader();

                while (getNames.Read())
                {
                    comboBox1.Items.Add(getNames["Naam"].ToString());
                }

                getNames.Close();
            }

            grabConnection.Close();
        }
    }
}
